<?php

/**
 * Yii Controller ShopController
 *
 * @author trangnguyen
 * @since Apr 17, 2014 - 3:39:24 PM
 */
class ShopController extends MyController
{

    public function actionIndex()
    {
        $this->product = true;
        $cate          = array();

        $url  = isset($_GET['url']) ? $_GET['url'] : null;
        $code = isset($_GET['color']) ? $_GET['color'] : null;
        //lay cate theo $url
        if ($url)
        {
            $cate = Category::model()->find('url = :u', array(':u' => $url));
        }
        $name              = ($cate) ? $cate->name : ' Cửa hàng';
        $this->breadcrumbs = array(
            $name,
        );
        //lay group product
        $cri               = new CDbCriteria();
        if (!empty($cate))
        {
            $cri->condition = 'status != :s and category_id = :c and type=:t';
            $cri->params    = array(
                ':s' => Category::STATUS_DRAFT,
                ':c' => $cate->id,
                ':t' => ProductGroup::TYPE_PRODUCT
            );
        }
        else
        {
            $cri->condition = 'status != :s and type=:t';
            $cri->params    = array(
                ':s' => Category::STATUS_DRAFT,
                ':t' => ProductGroup::TYPE_PRODUCT
            );
        }
        $cri->order = 'status DESC,id DESC';

        $produc_group = ProductGroup::model()->findAll($cri);

        //lay mang id cua $produc_group
        $id_group_arr = array();

        foreach ($produc_group as $value)
        {
            $id_group_arr[] = $value->id;
        }
        //lay product theo group
        $crit = new CDbCriteria();
        //neu select theo color
        if ($code)
        {
            $crit->condition = 'color_id = :c';
            $crit->params    = array(':c' => $code);
        }

        $crit->addInCondition('group_id', $id_group_arr);
        $crit->order = 'id DESC';

        $count           = Product::model()->count($crit);
        $pages           = new CPagination($count);
        $pages->route    = "$this->route";
        $pages->pageSize = 24;
        $pages->pageVar  = 'page';
        $pages->applyLimit($crit);

        $products = Product::model()->findAll($crit);
        //lay ma mau trong bang color
        $colors   = Color::model()->findAll();
        $this->render('index', array(
            'cate'     => $cate,
            'products' => $products,
            'color'    => $colors,
            'count'    => $count,
            'pages'    => $pages,
        ));
    }

    public function getProduct($color = NULL)
    {
        $url = isset($_GET['url']) ? $_GET['url'] : null;
        if (!$url)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        //lay product theo $url
        if (!$color)
        {
            $product = Product::model()->find('url = :u', array(':u' => $url));
        }
        else
        {
            $product = Product::model()->find('url = :u and color_id= :c', array(':u' => $url, ':c' => $color));
        }
        if (!$product)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $product;
    }

    public function actionView()
    {
        unset(Yii::app()->session['offset_product']);
        //lay product theo $url
        $product = $this->getProduct();

        // san pham cua product lien quan trong cung danh muc
        $p_group = ProductGroup::model()->find('category_id =:c and id !=:i and type=:t', array(
            ':c' => $product->group->category_id,
            ':i' => $product->group->id,
            ':t' => ProductGroup::TYPE_PRODUCT
        ));

        //lay array size_id 
        $size_arr = ProductSize::model()->findAll('pid =:pid', array(
            ':pid' => $product->id,
        ));
        $arr_size = array();
        foreach ($size_arr as $val)
        {
            $arr_size[] = $val->size_id;
        }
        //size theo $arr_size
        $crite = new CDbCriteria();
        $crite->addInCondition('id', array_unique($arr_size));
        $sizes = Size::model()->findAll($crite);

        $this->breadcrumbs = array(
            $product->group->category->name => $product->group->category->publicUrl,
            $product->group->name,
        );

        //san pham lienn quan

        $arr_group = $this->getArrayGroup();

        $crit           = $this->__criteria($product, $arr_group, FALSE);
        $all_p_relation = Product::model()->count($crit);

        $cri        = $this->__criteria($product, $arr_group, TRUE);
        $p_relation = Product::model()->findAll($cri);


        $this->render('view', array(
            'product'        => $product,
            'p_group'        => $p_group,
            'sizes'          => $sizes,
            'all_p_relation' => $all_p_relation,
            'p_relation'     => $p_relation,
        ));
    }

    public function __criteria($product, $arr_group, $type_relation = TRUE)
    {
        $cri            = new CDbCriteria();
        $cri->condition = 'id != :i ';
        $cri->params    = array(
            ':i' => @$product->id,
        );
        $cri->addInCondition('group_id', $arr_group);
        $cri->order     = ' id DESC';
        if ($type_relation)
        {
            $cri->limit = Option::getItem('limit_product');
        }
        return $cri;
    }

    public function getArrayGroup()
    {
        $product   = $this->getProduct();
        $p_groups  = ProductGroup::model()->findAll('category_id = :i', array(':i' => $product->group->category_id));
        $arr_group = array();
        foreach ($p_groups as $val)
        {
            $arr_group[] = $val->id;
        }
        return $arr_group;
    }

    public function actionAjaxLoadProduct()
    {
        if (isset(Yii::app()->session['offset_product']))
        {
            Yii::app()->session['offset_product']+=Option::getItem('limit_product');
        }
        else
        {
            Yii::app()->session['offset_product'] = Option::getItem('limit_product');
        }

        $arr_group   = $this->getArrayGroup();
        //lay dieu kien nhu index
        $cri         = new CDbCriteria();
        $cri->addInCondition('group_id', $arr_group);
        $cri->limit  = Option::getItem('limit_product');
        $cri->offset = Yii::app()->session['offset_product'];
        $cri->order  = ' id DESC';

        $product = Product::model()->findAll($cri);

        $cri         = new CDbCriteria();
        $cri->addInCondition('group_id', $arr_group);
        $all_product = Product::model()->count($cri);

        $next = Yii::app()->session['offset_product'] + Option::getItem('limit_product') >= $all_product ? FALSE : TRUE;
        $data = $this->renderPartial('page_ajax', array('p_relation' => $product), true);

        echo json_encode(array(
            'next' => $next,
            'data' => $data
        ));
    }

    public function actionAjaxLoadImage()
    {
        $color = isset($_GET['color']) ? $_GET['color'] : null;
        $image = isset($_GET['image']) ? $_GET['image'] : null;


        //lay product theo $url
        $product = $this->getProduct();

        if ($color)
        {
            $p_ajax = Product::model()->find('color_id = :c and group_id =:g', array(
                ':c' => $color,
                ':g' => $product->group->id,
            ));
        }
        else
        {
            $p_ajax = ProductImage::model()->findByPk($image);
        }
        echo $this->renderPartial('load_img', array('product' => $p_ajax));
    }

    public function actionAjaxLoadP_Image()
    {
        $color = isset($_GET['color']) ? $_GET['color'] : null;

        //lay product theo $url
        $product = $this->getProduct();

        if ($color)
        {
            $p_ajax = Product::model()->find('color_id = :c and id =:i', array(
                ':c' => $color,
                ':i' => $product->id,
            ));
        }
        echo $this->renderPartial('load_pImage', array('product' => $p_ajax));
    }

    public function actionAjaxProductByColor()
    {
        $color   = isset($_GET['color']) ? $_GET['color'] : null;
        //lay product theo $url
        $product = $this->getProduct($color);

        $size_arr = ProductSize::model()->findAll('pid =:pid', array(
            ':pid' => $product->id,
        ));

        $arr_size = array();
        foreach ($size_arr as $val)
        {
            $arr_size[] = $val->size_id;
        }
        //size theo $arr_size
        $crite = new CDbCriteria();
        $crite->addInCondition('id', array_unique($arr_size));
        $sizes = Size::model()->findAll($crite);

        echo $this->renderPartial('load_size', array('sizes' => $sizes));
    }

    public function actionIcon()
    {
        $cate = array();
        $url  = isset($_GET['url']) ? $_GET['url'] : null;
        if ($url)
        {
            $cate = Category::model()->find('url = :u', array(':u' => $url));
        }
        $name              = ($cate) ? $cate->name : ' Kho hình';
        $this->breadcrumbs = array(
            $name,
        );
        $cri               = new CDbCriteria();
        if (!empty($cate))
        {
            $cri->condition = 'category_id = :c';
            $cri->params    = array(
                ':c' => $cate->id,
            );
        }

        $cri->order      = 'rank DESC,id DESC';
        $count           = Icon::model()->count($cri);
        $pages           = new CPagination($count);
        $pages->route    = "$this->route";
        $pages->pageSize = 24;
        $pages->pageVar  = 'page';
        $pages->applyLimit($cri);

        $icons = Icon::model()->findAll($cri);
        $this->render('index', array(
            'icons' => $icons,
            'cate'  => $cate,
            'count' => $count,
            'pages' => $pages,
        ));
    }

}

/** 
 * End file ShopController
 */
